package dbLayer;

import modelLayer.Product;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DBProduct implements DBProductIF {
	
    private  Connection con;

    public DBProduct() {
    	
    	this.con = DBConnection.getInstance().getDBcon();

    }
    
    public Product findProductID(int productID) {   
    	
    	String wClause = " productID = '" + productID + "'";     
    	return singleWhere(wClause);
    
    }

	public List<Product> findProductName(String productName) {   
    	
    	String wClause = " name like '%" + productName + "%'";
    	return miscWhere(wClause);
    
    }
		
	private Product singleWhere(String wClause) {
		
		ResultSet results;
		Product product = new Product();
        String query =  buildQuery(wClause);
        System.out.println(query);
		try { 
			Statement stmt = this.con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		if(results.next()) {
	 			product = buildObject(results);
                stmt.close();
			}
            else {
            	product = null;
            }
		}
	 	catch(Exception e) {
	 		System.out.println("Query exception: " + e);
	 	}
		return product;

	}
	
	private List<Product> miscWhere(String wClause) {
		
		ResultSet results;
		List<Product> matchingProducts = new ArrayList<Product>();		
        String query =  buildQuery(wClause);
        System.out.println(query);
		try { 
			Statement stmt = this.con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		while(results.next()) {
	 			Product product = new Product();
	 			product = buildObject(results);
	 			matchingProducts.add(product);
			}
	 		stmt.close();
		}
	 	catch(Exception e) {
	 		System.out.println("Query exception: " + e);
	 	}
		return matchingProducts;
	}

	private String buildQuery(String wClause) {
		
		String query = "SELECT productID, supplierID, name, purchasePrice, salesPrice, rentPrice, countryOfOrigin, " +
					   "type, description, size, colour, material, calibre, minStock FROM Product";		
		if (wClause.length() > 0) {
			query = query + " WHERE" + wClause;
		}		
		return query;
		
	}
	
	private Product buildObject(ResultSet results) {   
		
		Product product = null;
        try {
        	product = new Product(results.getInt("productID"),
        						  results.getInt("minStock"),
        						  results.getDouble("purchasePrice"),
        						  results.getDouble("salesPrice"),
        						  results.getDouble("rentPrice"),
        						  results.getDouble("calibre"),
        						  results.getString("name"),
        						  results.getString("countryOfOrigin"),
        						  results.getString("type"),
        						  results.getString("description"),
        						  results.getString("size"),
        						  results.getString("colour"),
        						  results.getString("material"),
        						  null);       								         							
        }
        catch(Exception e) {
            System.out.println("Der opstod en fejl i oprettelsen af et produkt object.");
            System.out.println(e);
        }
        return product;
        
	}
 
}  